package com.zx.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zx.sys.constast.SysConstast;
import com.zx.sys.service.RoleService;
import com.zx.sys.utils.DataGridView;
import com.zx.sys.utils.ResultObj;
import com.zx.sys.vo.RoleVo;

@RestController
@RequestMapping("role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;

	@RequestMapping("loadAllRole")
	public DataGridView loadAllRole(RoleVo roleVo){
		return roleService.queryAllRole(roleVo);
	}
	
	@RequestMapping("addRole")
	public ResultObj addRole(RoleVo roleVo){
		try {
			
			roleService.addRole(roleVo);
			return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.ADD_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.ADD_ERROR);
		}
	}
	
	@RequestMapping("updateRole")
	public ResultObj updateRole(RoleVo roleVo){
		try {
			roleVo.setRoleid(null);
			roleService.updateRole(roleVo);
			return new ResultObj(SysConstast.CODE_SUCCESS, SysConstast.UPDATE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR, SysConstast.UPDATE_ERROR);
		}
	}
	
	@RequestMapping("deleteRole")
	public ResultObj deleteRole(RoleVo roleVo){
		try {
			roleService.deleteRole(roleVo.getRoleid());
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DELETE_ERROR);
		}
	}
	
	@RequestMapping("deleteBatchRole")
	public ResultObj deleteBatchRole(Integer[] ids){
		try {
			roleService.deleteBatchRole(ids);
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DELETE_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DELETE_ERROR);
		}
	}
	
	@RequestMapping("initRoleMenuTreeJson")
	public DataGridView initRoleMenuTreeJson(Integer roleid){
		return roleService.initRoleMenuTreeJson(roleid);
	}
	
	@RequestMapping("saveRoleMenu")
	public ResultObj saveRoleMenu(RoleVo roleVo){
		try {
			roleService.saveRoleMenu(roleVo);
			return new ResultObj(SysConstast.CODE_SUCCESS,SysConstast.DISPATCH_SUCCESS);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new ResultObj(SysConstast.CODE_ERROR,SysConstast.DISPATCH_ERROR);
		}
	}
}
